MEGpipeline_TimelockFreq.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Wrapper for timelock & frequency analysis. %
  3. % Last modified: Jan. 15, 2014 %
  4. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  5. % Copyright (C) 2013-2014, Michael J. Cheung
  6. %
  7. % This file is a part of the MEG & PLS Pipeline (MEGPLS). For more
  8. % details, see the documentation included with the software package.
  9. %
  10. % MEGPLS is free software: you can redistribute it and/or modify it under
  11. % the terms of the GNU General Public License version 2 as published by
  12. % the Free Software Foundation. This program is distributed in the hope
  13. % that it will be useful, but WITHOUT ANY WARRANTY; without even the
  14. % implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. % See the GNU General Public License for more details.
  16. %
  17. % You should have received a copy of the GNU General Public License along
  18. % with this program. If not, you can download the license here:
  19. % <http://www.gnu.org/licenses/old-licenses/gpl-2.0>.
  20. function MEGpipeline_TimelockFreq(BuilderMat, AnalysisType)
  21. % Make sure java paths added:
  22. UseProgBar = CheckParforJavaPaths;
  23. % Load builder .mat file:
  24. Builder = load(BuilderMat);
  25. name = Builder.name;
  26. paths = Builder.paths;
  27. time = Builder.time;
  28. FTcfg = Builder.FTcfg;
  29. % Clear existing Errorlog & Diary:
  30. switch AnalysisType
  31. case 'Timelock'
  32. RunSection = 'TimelockAnalysis';
  33. case 'Freq'
  34. RunSection = 'FreqAnalysis';
  35. end
  36. if exist(['ErrorLog_',RunSection,'.txt'], 'file')
  37. system(['rm ErrorLog_',RunSection,'.txt']);
  38. end
  39. if exist(['Diary_',RunSection,'.txt'], 'file')
  40. system(['rm Diary_',RunSection,'.txt']);
  41. end
  42. diary(['Diary_',RunSection,'.txt']);
  43. ErrLog = fopen(['ErrorLog_',RunSection,'.txt'], 'a');
  44. %=====================================%
  45. % RUN TIMELOCK OR FREQUENCY ANALYSIS: %
  46. %=====================================%
  47. for g = 1:length(name.GroupID)
  48. NumSubj = length(name.SubjID{g});
  49. if UseProgBar == 1
  50. ppm = ParforProgMon...
  51. (['TIMELOCK / FREQUENCY ANALYSIS: ',name.GroupID{g},'. '], NumSubj, 1, 700, 80);
  52. end
  53. parfor s = 1:length(name.SubjID{g})
  54. for c = 1:length(name.CondID)
  55. CheckInput = CheckPipelineMat(paths.MEGdata{g}{s,c}, RunSection);
  56. if CheckInput == 0
  57. continue;
  58. end
  59. % Resize input data to analysis time:
  60. cfgResize = [];
  61. cfgResize.toilim = [time.Start, time.End];
  62. cfgResize.inputfile = paths.MEGdata{g}{s,c};
  63. ResizedData = ft_redefinetrial(cfgResize)
  64. % Run timelock & GMFP analysis:
  65. if strcmp(AnalysisType, 'Timelock')
  66. CheckSavePerms(paths.Timelock{g}{s,c}, RunSection);
  67. cfgTimelock = [];
  68. cfgTimelock = FTcfg.Timelock;
  69. cfgTimelock.outputfile = paths.Timelock{g}{s,c};
  70. cfgTimelock.outputfilepresent = 'overwrite';
  71. disp('Running timelock analysis:')
  72. ft_timelockanalysis(cfgTimelock, ResizedData)
  73. if exist(paths.Timelock{g}{s,c}, 'file')
  74. CheckSavePerms(paths.GlobalMeanField{g}{s,c}, RunSection);
  75. cfgGFP = [];
  76. cfgGFP = FTcfg.GFP;
  77. cfgGFP.inputfile = paths.Timelock{g}{s,c};
  78. cfgGFP.outputfile = paths.GlobalMeanField{g}{s,c};
  79. cfgGFP.outputfilepresent = 'overwrite';
  80. ft_globalmeanfield(cfgGFP) % Compute GMFP from new timelock file.
  81. end
  82. end
  83. % Run frequency analysis:
  84. if strcmp(AnalysisType, 'Freq')
  85. CheckSavePerms(paths.Freq{g}{s,c}, RunSection);
  86. cfgFreq = [];
  87. cfgFreq = FTcfg.Freq;
  88. cfgFreq.outputfile = paths.Freq{g}{s,c};
  89. cfgFreq.outputfilepresent = 'overwrite';
  90. disp('Running frequency analysis:')
  91. ft_freqanalysis(cfgFreq, ResizedData)
  92. end
  93. ResizedData = []; % Free memory
  94. end % Cond
  95. if UseProgBar == 1 && mod(s, 1) == 0
  96. ppm.increment(); % move up progress bar
  97. end
  98. end % Subj
  99. if UseProgBar == 1
  100. ppm.delete();
  101. end
  102. end % Group
  103. %=========================%
  104. % CHECK FOR OUTPUT FILES: %
  105. %=========================%
  106. for g = 1:length(name.GroupID)
  107. for s = 1:length(name.SubjID{g})
  108. for c = 1:length(name.CondID)
  109. switch AnalysisType
  110. case 'Timelock'
  111. if ~exist(paths.Timelock{g}{s,c}, 'file')
  112. fprintf(ErrLog, ['ERROR: Output timelock file missing:'...
  113. '\n %s \n\n'], paths.Timelock{g}{s,c});
  114. end
  115. if ~exist(paths.GlobalMeanField{g}{s,c}, 'file')
  116. fprintf(ErrLog, ['ERROR: Output global mean field file missing:'...
  117. '\n %s \n\n'], paths.GlobalMeanField{g}{s,c});
  118. end
  119. case 'Freq'
  120. if ~exist(paths.Freq{g}{s,c}, 'file')
  121. fprintf(ErrLog, ['ERROR: Output freq analysis file missing:'...
  122. '\n %s \n\n'], paths.Freq{g}{s,c});
  123. end
  124. end
  125. end % Cond
  126. end % Subj
  127. end % Group
  128. %=================%
  129. if exist([pwd,'/ErrorLog_',RunSection,'.txt'], 'file')
  130. LogCheck = dir(['ErrorLog_',RunSection,'.txt']);
  131. if LogCheck.bytes ~= 0 % File not empty
  132. open(['ErrorLog_',RunSection,'.txt']);
  133. else
  134. delete(['ErrorLog_',RunSection,'.txt']);
  135. end
  136. end
  137. fclose(ErrLog);
  138. diary off
  139. end